package com.food.restaurantpro.observers;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.ProgressDialog;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.connection.DataHelper;
import com.food.restaurantpro.connection.DataResult;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;
import com.food.restaurantpro.objects.PreOrderObject;

public class ListOrderAndDetailObserver implements Observer {

	private ProgressDialog _waitingDialog;
	private ListOrderAndDetailObserver _me = this;
	private boolean hadRegisterObserver = false;
	private ListOrderAndDetailObserverInterface mListOrderAndDetailObserverInterface;
	
	private Activity mActivity;

	public ListOrderAndDetailObserver(ListOrderAndDetailObserverInterface listOrderAndDetailObserverInterface) {
		mListOrderAndDetailObserverInterface = listOrderAndDetailObserverInterface;
	}

	/**
	 * register observer and getListOrderAndDetail
	 */
	public void registerObserver(Activity activity, String generateID) {
		mActivity = activity;
		if (ManagingNetwork.isNetworkAvailable()) {
			// show progress
			showProcessDialog();
			hadRegisterObserver = true;
			DataHelper.getInstance().addObserver(_me);
			DataHelper.getInstance().getListOrderAndDetail(generateID);

		} else {
			mListOrderAndDetailObserverInterface.listOrderAndDetailCallback(null,
					RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.NETWORK_ERROR));
		}
	}

	/**
	 * must call this method in onDestroy of Activity which called
	 * registerObserver
	 */
	public void unRegisterObserver() {
		if (hadRegisterObserver) {
			hideProcessDialog();
			DataHelper.getInstance().deleteObserver(_me);
			hadRegisterObserver = false;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// close progress
		if (data == null) {
			hideProcessDialog();
			return;
		}
			
		DataResult result = (DataResult) data;
		switch (result.getMethodName()) {
		case getListOrderAndDetail:
			if (result.getErrorCode() != null) {
				// show error mesage
				mListOrderAndDetailObserverInterface.listOrderAndDetailCallback(null, result
						.getErrorCode().getErrorMessage());
			} else {
				if (result.getData() != null) {
					//remove items with status = 0
					ArrayList<PreOrderObject> preOrderObjectList = (ArrayList<PreOrderObject>) result.getData();
					for (int i = 0; i < preOrderObjectList.size(); i++) {
						if(preOrderObjectList.get(i).getPreorderStatus().equalsIgnoreCase("0")) {
							preOrderObjectList.remove(i);
						}
					}
					mListOrderAndDetailObserverInterface.listOrderAndDetailCallback(preOrderObjectList, null);
				} else {
					mListOrderAndDetailObserverInterface.listOrderAndDetailCallback(null, null);
				}
			}
			unRegisterObserver();
			break;
		default:
			break;
		}
		// Chu y: Observer se post notification den tat ca cac lop da register
		// observer. Va cho nao co phuong thuc trung voi phuong thuc no tra ve
		// thi xu ly
		// cho do, nhung cho khac ko xu ly, vi vay PHAI dat code o trong
		// "case method:"
		// khong dat code o ngoai nay
	}

	private void showProcessDialog() {
		if (_waitingDialog == null || !_waitingDialog.isShowing()) {
			_waitingDialog = new ProgressDialog(mActivity);
			_waitingDialog.setMessage(mActivity.getString(R.string.processing));
			_waitingDialog.setCancelable(false);
			_waitingDialog.show();
		}
	}

	//Ham nay phai duoc goi trong onDestroy
	//Vi co truong hop dang request thi destroy activity
	//dialog se ko duoc dong neu ko dat ham nay trong onDestroy
	private void hideProcessDialog() {
		if (_waitingDialog != null && _waitingDialog.isShowing()) {
			_waitingDialog.dismiss();
		}
	}

}
